package com.andy.qhb.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.andy.qhb.QhbApplication;
import com.andy.qhb.base.HttpClientUtil;
import com.andy.qhb.base.PageUtil;
import com.andy.qhb.base.PageVO;
import com.andy.qhb.base.RespVO;
import com.andy.qhb.base.form.FormField;
import com.andy.qhb.base.table.PageStructure;
import com.andy.qhb.base.table.Search;
import com.andy.qhb.base.table.TableHeader;
import com.andy.qhb.base.table.Tool;
import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.dao.vo.Where;
import com.andy.qhb.entity.ConfCommon;
import com.andy.qhb.entity.UsrGameInfo;
import com.andy.qhb.entity.UsrUser;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.utils.GoConstant;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("admin/user")
public class UsrUserController extends BaseController{

	/**
	 * 用户列表：结构
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="list")
	public ModelAndView list() throws Exception {
		TableHeader tableHeader = new TableHeader();
		tableHeader.setNames(new String[]{"id", "nick_name", "coin:yuan", "win_count","role"});
		tableHeader.setTexts(new String[]{"ID", "昵称","金币","没踩雷次数","角色"});
		
		Tool tool = new Tool();
		tool.setUrls("admin/user/toAddCoin","admin/user/setAgent");
		tool.setTexts("增加金币","设置为代理");
		tool.setTypes("edit","del");
		
		Search search = new Search();
		search.setNames(new String[]{"uid"});
		search.setTexts(new String[]{"玩家ID"});
		search.setTypes(new String[]{"text"});
		
		PageStructure data = PageUtil.createTablePageStructure("admin/user/listData", "id", tableHeader,tool,search);
		return createMV("tableList","用户管理", Collections.singletonMap("formStruct", data));
	}
	
	/**
	 * 用户列表：数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="listData")
	public RespVO listData(Integer page,Integer limit, String uid) throws Exception {
		PageVO users = null;
		if(StringUtils.isNotBlank(uid)){
			users = DaoUtils.getPageForMap(UsrUser.table(), null,page,limit,Where.eq("id", uid));
		}else{
			users = DaoUtils.getPageForMap(UsrUser.table(), null,page,limit);
		}
		users.getItems().forEach((it)->{
			Integer role = MapUtils.getInteger(it, "role");
			Integer agent = MapUtils.getInteger(it, "agent");
			it.put("role", role == 0? (agent == 0?"玩家":"代理"):"机器人");
		});
		idToName(users.getItems(), UsrGameInfo.table(), "id#uid:coin,win_count");
		
        return RespVO.createSuccessJsonResonse(users);
	}
	
	/**
	 * 增加金币:界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="toAddCoin")
	public ModelAndView toAddCoin(Long id) throws Exception {
		List<FormField> formFieldList = new ArrayList<>();
		formFieldList.add(FormField.builder().name("uid").text("玩家ID").type("span").build());
		formFieldList.add(FormField.builder().name("coin").text("拥有金币").type("span").build());
		formFieldList.add(FormField.builder().name("addCoin").text("增加金币").build());
		UsrGameInfo gameInfo = DaoUtils.getOne(UsrGameInfo.class,Where.eq("uid", id));
		Map<String, Object> data = PageUtil.createFormPageStructure("admin/user/addCoin", formFieldList,gameInfo);
		
		return createCustMV("window/add",data);
	}
	
	/**
	 * 增加金币：执行
	 * @param current_page
	 * @param areas
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addCoin")
	public RespVO addCoin(Long id,Long uid,Integer addCoin) throws Exception {
		if(QhbApplication.gameServerEnable()){
			GameUtils.addCoin(uid, addCoin*100);
		}else{
			ConfCommon conf = DaoUtils.getOne(ConfCommon.class, Where.eq("nid", "game_server_url"));
			ConfCommon secKey = DaoUtils.getOne(ConfCommon.class, Where.eq("nid", "commu_seckey"));
			Map<String,Object> params = Maps.newHashMap();
			params.put("seckey", secKey.getValue());
			params.put("uid", uid);
			params.put("addCoin", addCoin*100);
			HttpClientUtil.post(conf.getValue()+"admin/cmd/addCoin/"+id, params, null);
		}
		
        return RespVO.createSuccessJsonResonse("增加成功");
	}
	
	/**
	 * 设置为代理
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="setAgent")
	public RespVO setAgent(Long id) throws Exception {
		if(id == null) return null;
		
		UsrUser user = DaoUtils.get(id, UsrUser.class);
		if(user == null) RespVO.createErrorJsonResonse("用户不存在");
		
		if(user.getId() < GoConstant.ROBOT_ID_END) {
			return RespVO.createErrorJsonResonse("不可设置机器人为代理");
		}
		if(user.getPuid() > 0) {
			return RespVO.createErrorJsonResonse("用户不是顶级用户");
		}
		if(user.getAgent() > 0) {
			return RespVO.createErrorJsonResonse("用户已经为代理，不可重复设置");
		}
		
		
		user.setAgent(1);
		DaoUtils.update(user);
		
		return RespVO.createSuccessJsonResonse("设置成功");
	}

}
